var box=document.getElementById("box");
var start=document.getElementById("start");
var flyBird=document.getElementById("flyBird");
var scoreBoard=document.getElementById("scoreBoard");
var ul=document.getElementsByTagName("ul")[0];
var audios=document.getElementsByTagName("audio");
var downTimer=null;
var upTimer=null;
var speed=0;
var maxSpeed=8;
var pipeTimer=null;
var scorenum=0;
var crashTimer=0;
function downBird(){
	flyBird.src="img/down_bird0.png";
	speed+=.3;
	if(speed>=maxSpeed){
		speed=maxSpeed
	}
	flyBird.style.top=flyBird.offsetTop+speed+"px";
}
function peng(a,b){
	return !(b.offsetLeft+b.offsetWidth<=a.parentNode.offsetLeft
		||b.offsetLeft>=a.parentNode.offsetLeft+a.parentNode.offsetWidth
		||a.offsetTop>=b.offsetTop+b.offsetHeight
		||b.offsetTop>=a.offsetTop+a.offsetHeight)
}
function random(min,max){
	return Math.random()*(max-min)+min;
}
function addScore(){
	scorenum++;
	console.log(scorenum);
	var scorenumstr=scorenum+"";
	scoreBoard.innerHTML="";
	for(var i=0;i<scorenumstr.length;i++){
		var img=document.createElement("img");
		img.src=`img/${scorenumstr[i]}.jpg`;
		scoreBoard.appendChild(img);
	}
}
function gameOver(){
	console.log("game over");
	clearInterval(downTimer);
	clearInterval(upTimer);
	clearInterval(pipeTimer);
	clearInterval(crashTimer);
	audios[2].play();
	audios[1].pause();
	var lis=document.getElementsByTagName("li");
	for(var i=0;i<lis.length;i++){
		clearInterval(lis[i].moveTimer)
	}
	box.onclick=null;
}
function createPipe(){
	var li=document.createElement("li");
	var topHeight=random(70,230);
	var bottomHeight=300-topHeight;
	li.innerHTML=`<div class="pipeTop" style="height:${topHeight}px">
					<img src="img/up_pipe.png"/>
					</div>
					<div class="pipeBottom" style="height:${bottomHeight}px">
					<img src="img/down_pipe.png"/>
					</div>`;
	ul.appendChild(li);
	li.lock=false;
 //    var pipeTop=document.getElementsByClassName("pipeTop")[0];
	// var pipeBottom=document.getElementsByClassName("pipeBottom")[0];
	 var pipeTop=li.firstChild;
	var pipeBottom=li.lastChild;
	console.log(pipeTop);
	li.moveTimer=setInterval(
		function(){	
			li.style.left=`${li.offsetLeft-3}px`;
			if(li.offsetLeft<-70){
				ul.removeChild(li);
			}
			if(flyBird.offsetLeft>li.offsetLeft+li.offsetWidth&&li.lock==false){
				addScore();
				li.lock=true;
			}
			if (peng(pipeTop,flyBird)||peng(pipeBottom,flyBird)) {
				gameOver();
			}
		},30)
}

function gameStart(){
	start.style.display="none";
	title.style.display="none";
	flyBird.style.display="block";
	downTimer=setInterval(function(){
		downBird()
	},30)
	//出现管道
	pipeTimer=setInterval(createPipe,2000);
	audios[1].play();
	crashTimer=setInterval(function(){
		if(flyBird.offsetTop<=0||flyBird.offsetTop+flyBird.offsetHeight>=422){
			gameOver();
		}
	},30)
	
}
start.onclick=function(event){
	var ev=event||window.event;
	if(ev.stopPropagation){
		ev.stopPropagation();
	}else{
		ev.cancelBubble=true;
	}
	gameStart();
	box.onclick=function(){
		clearInterval(downTimer);
		clearInterval(upTimer);
		audios[0].pause();
		audios[0].play();
		flyBird.src="img/up_bird1.png";
		speed=maxSpeed;
		upTimer=setInterval(function(){
			speed-=.7;
			if(speed<=0){
				clearInterval(upTimer);
				downTimer=setInterval(function(){
					downBird()
				},30)
			}
			flyBird.style.top=flyBird.offsetTop-speed+"px"
		},30)
	}
}